<?php 
class ControllerAccountHistory extends Controller {	
	function index() {
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('account_history'));

	  		$response->redirect($url->ssl('account_login'));
    	}
 
		//pagination
        $session->set('account_history.page', $request->has('page')?(int)$request->get('page'):1);

    	$language->load('controller/account_history.php');

    	$template->set('title', $language->get('heading_title'));

		$results = $database->getRows($database->splitQuery("select o.order_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_added, o.total, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where customer_id = '" . (int)$customer->getId() . "' and os.language_id = '" . (int)$language->getId() . "' order by order_id desc", $session->get('account_history.page'), $config->get('config_max_rows')));
		
		$view = $this->locator->create('template');

		$view->set('tpl', $template);

      	$view->set('heading_title', $language->get('heading_title'));
		
		if ($results) {

      		$view->set('text_order', $language->get('text_order'));
      		$view->set('text_status', $language->get('text_status'));
     		$view->set('text_date_added', $language->get('text_date_added'));
      		$view->set('text_customer', $language->get('text_customer'));
      		$view->set('text_products', $language->get('text_products'));
      		$view->set('text_total', $language->get('text_total'));
     		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

      		$view->set('entry_page', $language->get('entry_page'));

      		$view->set('button_view', $language->get('button_view'));
      		$view->set('button_continue', $language->get('button_continue'));

			$view->set('action', $url->href('account_history', 'page'));

			$order_data = array();

      		foreach ($results as $result) {
        		$product_info = $database->getRow("select count(*) as products from order_product where order_id = '" . (int)$result['order_id'] . "'");

        		$order_data[] = array(
          			'reference'  => $result['reference'],
          			'href'       => $url->ssl('account_invoice', FALSE, array('order_id' => $result['order_id'])),
          			'name'       => $result['firstname'] . ' ' . $result['lastname'],
          			'status'     => $result['status'],
          			'date_added' => $language->formatDate($language->get('date_format_short'), strtotime($result['date_added'])),
          			'products'   => $product_info['products'],
          			'total'      => $currency->format($result['total'], $result['currency'], $result['value'])
        		);
      		}

      		$view->set('orders', $order_data);

      		$view->set('page', $session->get('account_history.page'));

      		$page_data = array();

      		for ($i = 1; $i <= $database->getPages(); $i++) {
					$query=array('path' => $request->gethtml('path'));
					if ($i > 1) $query['page'] = $i;
        			$page_data[] = array(
          				'text'  => $language->get('text_pages', $i, $database->getPages()),
						'href'	=> $url->href('account_history', FALSE, $query),
          				'value' => $i
        			);
      		}

		  	$view->set('pages', $page_data);

      		$view->set('continue', $url->ssl('account'));
	  
	  		$template->set('content', $view->fetch('content/account_history.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));
    	} else {

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->ssl('account'));

	  		$template->set('content', $view->fetch('content/error.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));
		}
	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');

		//backwards compat pagination
		$query=array();

		$query['path'] = $request->gethtml('path');

		//backwards compat pagination
      	if ($request->has('page', 'post')) {
			$i=(int)$request->get('page', 'post');
			if ($i > 1) $query['page'] = $i;
      	}

		$response->redirect($url->href('account_history', FALSE, $query));
	}
}
?>
